
#include "StateMuseum.h"
#include "StateMainMenu.h"
#include "StateManager.h"
#include "InputManager.h"
#include "AudioUtils.h"
#include "ProgressManager.h"

void StateMuseum::loadContent()
{
    m_oBg = new RenderableEntity("MENU_0010_bg", Vector2(320, 480));
    m_oBg->setCentered();
    
    m_oTitleBg = new RenderableEntity("MENU_0009_ribbon", Vector2(320, 870));
    m_oTitleBg->setCentered();
    m_oTitleBg->setLiving(true);
    
    m_oTitleText = new TextEntity("Pacifier Museum", FONT_NAME, 48, Vector2(320, 870), 1, 1);
    m_oTitleText->setCentered();
    m_oTitleText->setColor(COLOR_PINK_TEXT);

    for(int i=0; i<NUM_PAGES; ++i)
    {
        m_oItemsPage[i] = new Container();
        m_oItemsPage[i]->setPosition(Vector2(640 * i, 0));
        
        m_oItemsBg[i] = new RenderableEntity("MENU_0006_dialog", Vector2(320, -50));
        m_oItemsBg[i]->setCenter(Vector2(0.5f, 0.0f));
        m_oItemsBg[i]->setScale(Vector2(1.0f, 1.05f));
        
        m_oItemsPage[i]->addEntity(m_oItemsBg[i]);
    }

    for(int i=0; i<MAX_ACHIEVEMENTS; ++i)
    {
        int iPage = (i / 9);
        int iRow = (i % 9) / 3;
        int iCol = (i % 9) % 3;
        m_oItems[i] = new RenderableEntity(ProgressManager::instance().getAchievementPacifier(i), Vector2(170 + iCol * 150, 680 - iRow * 170));
        m_oItems[i]->setScale(Vector2(0.9, 0.9));
        m_oItems[i]->changeColorWhenClicked(true);
        m_oItems[i]->setupButton(m_oItems[i]->getSize() * 0.2f, m_oItems[i]->getSize() * 0.6f);
        m_oItems[i]->setCenter(Vector2(0.5f, 0.5f));
        
        m_oItemsPage[iPage]->addEntity(m_oItems[i]);
        
        //if(!ProgressManager::instance().isAchievementDone(i))
        //{
        //    m_oItems[i]->setColor(COLOR_PINK_DARK);
        //}
    }
    
    m_oName = new TextEntity("Shait!", FONT_NAME, 36, Vector2(320, 120), 1, 1);
    m_oName->setShadow(true, COLOR_PINK_DARK);
    m_oName->setCenter(Vector2(0.5f, 0.0f));
    
    m_oDescription = new TextEntity("Feed 50 babies to get this pacifier!", FONT_NAME, 28, Vector2(180, 120), 0, 0, true, 380);
    m_oDescription->setCenter(Vector2(0.0f, 1.0f));

    m_oItemMarker = new RenderableEntity("selectedpacifier", Vector2(0.0f, 0.0f));
    m_oItemMarker->setCentered();
    m_oItemMarker->setLiving(true);
    
    int iDistX = 35;
    int iInitX = 320 - (NUM_PAGES-1) * iDistX * 0.5;
    for(int i=0; i<NUM_PAGES; ++i)
    {
        m_oPages[i] = new RenderableEntity("MENU_0007_score", Vector2(iInitX + (iDistX * i), 230));
        m_oPages[i]->setScale(Vector2(0.1, 0.1));
        m_oPages[i]->setColor(m_iPage == i ? COLOR_WHITE : COLOR_GREY);
        m_oPages[i]->setCentered();
    }

    m_oBack = new RenderableEntity("MENU_0008_back", Vector2(20, 20));
    m_oBack->changeColorWhenClicked(true);
    m_oBack->setLiving(true);

    m_oEnter = new TimeLineEffect();
    
    m_oExit = new TimeLineEffect();
    
    m_fPageDisplacement = 0;
}

void StateMuseum::unloadContent()
{
    delete m_oBg;
}

void StateMuseum::enter()
{    
    m_eState = MUSEUM_ENTER;
    m_iPage = 0;
    m_iPacifier = -1;
    m_oEnter->start(true);
    m_fPageDisplacement = 0;
    
//    for(int i=0; i<MAX_ACHIEVEMENTS; ++i)
//    {
//        if(!ProgressManager::instance().isAchievementDone(i))
//        {
//            m_oItems[i]->setColor(COLOR_PINK_DARK);
//        }
//        else 
//        {
//            m_oItems[i]->setColor(COLOR_WHITE);
//        }
//    }
}

void StateMuseum::update(float dt)
{
    m_oTitleBg->update();
    m_oBack->update();
    
    switch(m_eState)
    {
        case MUSEUM_ENTER:
            if(m_oEnter->isFinished())
            {
                m_eState = MUSEUM_IDLE;
            }
            break;
            
        case MUSEUM_IDLE:
            
            if(m_oItemsBg[m_iPage]->isPressed() && InputManager::instance().hasJustMoved())
            {
                m_oItems[m_iPacifier]->setLiving(false);
                m_iPacifier = -1;
                
                m_eState = MUSEUM_TOUCHING;
                m_vLastMousePos = InputManager::instance().getPosition();
                m_fTouchTime = SB::gameTime;
                break;
            }

            for(int i=0; i<MAX_ACHIEVEMENTS; ++i)
            {
                int iPage = (i / 9);
                if(iPage != m_iPage)
                    continue;
                
                m_oItems[i]->update();
                if(m_oItems[i]->isClicked())
                {
                    m_iPacifier = i;
                    m_oName->setText(ProgressManager::instance().getAchievementName(i));
                    m_oDescription->setText(ProgressManager::instance().getAchievementDesc(i));
                    m_oItems[m_iPacifier]->setLiving(true);
                    
                    m_oItemMarker->setPosition(m_oItems[m_iPacifier]->getPosition());
                    
                    break;
                }
            }
            
            if(m_iPacifier != -1)
            {
                m_oItemMarker->update();
            }
            
            if(m_oBack->isClicked())
            {
                m_oExit->start(true);
                m_eState = MUSEUM_EXIT;
            }
            break;
            
        case MUSEUM_TOUCHING:
        {
            float fCurrentPos = InputManager::instance().getPosition().x;
            float fx = (fCurrentPos - m_vLastMousePos.x) * 2;
            m_vLastMousePos.x = fCurrentPos;
            m_fPageDisplacement -= fx;
            
            if(InputManager::instance().hasJustReleased())
            {
                //Check time paging
                float m_fDisplaced = m_fPageDisplacement - (m_iPage * 640);
                float fTime = SB::gameTime - m_fTouchTime;
                float fSpeed = m_fDisplaced / fTime;
                bool bTimePaging = fabs(fSpeed) > 10 && fabsf(m_fDisplaced) > 10 && fTime < 0.5f;
                if(bTimePaging)
                {
                    if(fSpeed < 0 && m_iPage > 0)
                    {
                        m_iPage--;
                    }
                    else if(fSpeed > 0 && m_iPage < NUM_PAGES-1)
                    {
                        m_iPage++;
                    }
                }
                else 
                {
                    m_iPage = fmaxf(0, fminf(NUM_PAGES - 1, ((int)m_fPageDisplacement + 320) / 640));
                }
                
                //Set page marker
                for(int i=0; i<NUM_PAGES; ++i)
                {
                    m_oPages[i]->setColor(m_iPage == i ? COLOR_WHITE : COLOR_GREY);
                }
                
                m_fInitPageDisplacement = m_fPageDisplacement;
                m_fInitDisplacementTime = SB::gameTime;
                
                m_eState = MUSEUM_PAGING;
            }
            else
            {
                for(int i=0; i<NUM_PAGES; ++i)
                {
                    m_oItemsPage[i]->setPosition(Vector2(640 * i - m_fPageDisplacement, 0));
                }
            }
            break;
        }
        case MUSEUM_PAGING:
        {
            for(int i=0; i<NUM_PAGES; ++i)
            {
                if(m_oItemsBg[i]->isPressed() && InputManager::instance().hasJustMoved())
                {
                    m_eState = MUSEUM_TOUCHING;
                    m_vLastMousePos = InputManager::instance().getPosition();
                    m_fTouchTime = SB::gameTime;
                    break;
                }
            }

            float fPerc = fminf(1.0, (SB::gameTime - m_fInitDisplacementTime) / 0.3f);
            float fPageDisplacementRequired = m_iPage * 640;
            
            if(fPerc < 0.95f)
            {
                fPerc /= 0.90f;
                m_fPageDisplacement = m_fInitPageDisplacement + (fPageDisplacementRequired - m_fInitPageDisplacement) * powf(fPerc, 0.7f);
            }
            else if(fPerc < 1)
            {
                fPerc = 1.05f - 0.05f * (fPerc - 0.95f) / 0.05f;
                m_fPageDisplacement = m_fInitPageDisplacement + (fPageDisplacementRequired - m_fInitPageDisplacement) * powf(fPerc, 0.7f);
            }
            else
            {
                m_fPageDisplacement = fPageDisplacementRequired;
                m_eState = MUSEUM_IDLE;
            }
            
            for(int i=0; i<NUM_PAGES; ++i)
            {
                m_oItemsPage[i]->setPosition(Vector2(640 * i - m_fPageDisplacement, 0));
            }
            
            break;
        }
        case MUSEUM_EXIT:
            if(m_oExit->isFinished())
            {
                StateManager::instance().setState(StateMainMenu::instance());
            }
            
            break;
    }
}

void StateMuseum::render()
{
    m_oBg->render();

    m_oTitleBg->render();
    m_oTitleText->render();
    
    if(m_iPacifier != -1)
    {
        m_oItemMarker->render();
        m_oName->render();
        m_oDescription->render();
    }
    
    for(int i=0; i<NUM_PAGES; ++i)
    {
        m_oItemsPage[i]->render();
        m_oPages[i]->render();
    }

    m_oBack->render();
}

void StateMuseum::exit()
{
}
